﻿//***********************************************************************
//* Module:  TbBstFastprocessDAL
// * 作者:  曹荣芝
//* Modified: 2013-05-06
// * Purpose: Definition of the Class TbBstFastprocessDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.Framework.AuthEntity;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data.OracleClient;
namespace XWG.JWBST.JWBSTDAL
{
    /// <summary>
    /// 
    /// </summary>
    public class TbBstFastprocessDAL
    {
        #region 删除操作
        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="fastid">要删除的的主键值。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object fastid, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                JWBSTEntity.TbBstFastprocess u = JWBSTDAL.TbBstFastprocessDAL.Load(fastid) as JWBSTEntity.TbBstFastprocess;

                if (isLog)//如果写入日志
                {
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "删除";
                    log.LogObject = TbBstFastprocessConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransDelete<TbBstFastprocess, AppLog>(u, log);
                }
                else//不写入日志
                {
                    Db.TransDelete(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(fastid);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(fastid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;

            }
        }
        #endregion

        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="fastid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstFastprocess Load(object fastid)
        {
            return Db.GetModel<JWBSTEntity.TbBstFastprocess>(fastid);
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstFastprocess> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstFastprocess));
                //crt.Add(NHibernate.Criterion.Expression.Eq(TbBstFastprocessConst.Columns.EnabledPropertyName, Enabled.TRUE));
                IList<TbBstFastprocess> list = crt.List<TbBstFastprocess>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion
        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstFastprocess info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {

                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "新建";
                    log.LogObject = TbBstFastprocessConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    filedkey = Db.TransInsert<TbBstFastprocess, AppLog>(info, log);
                }
                else
                {
                    filedkey = Db.TransInsert<TbBstFastprocess>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstFastprocess info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                if (isLog)
                {


                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "修改";
                    log.LogObject = TbBstFastprocessConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransUpdate<TbBstFastprocess, AppLog>(info, log);
                }
                else
                {
                    Db.TransUpdate<TbBstFastprocess>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的修改操作");
                return resultModel;
            }
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询-根据code
        /// </summary>
        public static IList<TbBstDic> GetDictionaryByCode(string Parentnode)
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstDic));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.ParentnodePropertyName, Parentnode));
                IList<TbBstDic> list = crt.List<TbBstDic>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 查询-根据code和Parentnode
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public static IList<TbBstDic> GetDicByCodeAndParentnode(string Code, string Parentnode)
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstDic));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.ParentnodePropertyName, Parentnode));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.CodePropertyName, Code));
                IList<TbBstDic> list = crt.List<TbBstDic>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion

        #region 功能模块
        /// <summary>
        /// 获取最新流水号
        /// </summary>
        /// <returns></returns>
        public static string GetLastLSH()
        {
            string sql = "select max(FAST_LSH) from TB_BST_FASTPROCESS";
            return OrclHelper.ExecuteScalar(sql);
        }

        /// <summary>
        /// 上传音频路径
        /// </summary>
        /// <returns></returns>
        public static bool UpSound(string fastsound, string LSH)
        {
            string sql = "update tb_bst_fastprocess set fast_sound = :fastsound where fast_lsh = :lsh";
            OracleParameter[] sp = { 
                                   new OracleParameter(":fastsound",fastsound),
                                   new OracleParameter(":lsh",LSH)
                                   };
            int re = Convert.ToInt32(OrclHelper.ExecuteOracle(sql, sp));
            if (re != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据手机号查询历史
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static List<TbBstFastprocess> GetInfoByPhone(string phone)
        {
            List<TbBstFastprocess> fastlist = new List<TbBstFastprocess>();
            string sql = "select * from tb_bst_fastprocess where fast_phonea = :phone and FAST_COMPLETE = '1'";
            OracleParameter[] sp = { 
                                   new OracleParameter(":phone",phone)
                                   };
            DataTable mes = OrclHelper.GetDT(sql, sp);
            if (mes != null && mes.Rows.Count > 0)
            {
                for (int i = 0; i < mes.Rows.Count; i++)
                {
                    TbBstFastprocess fast = new TbBstFastprocess();
                    fast.FastLsh = mes.Rows[i]["fast_lsh"].ToString();
                    fast.FastAddress = mes.Rows[i]["FAST_ADDRESS"].ToString();
                    fast.FastLpna = mes.Rows[i]["FAST_LPNA"].ToString();
                    fast.FastLpnb = mes.Rows[i]["FAST_LPNB"].ToString();
                    fast.FastPhonea = mes.Rows[i]["FAST_PHONEA"].ToString();
                    fast.FastPhoneb = mes.Rows[i]["FAST_PHONEB"].ToString();
                    fast.FastUptime = Convert.ToDateTime(mes.Rows[i]["FAST_UPTIME"]);
                    fast.FastAudittime = Convert.ToDateTime(mes.Rows[i]["fast_audittime"].ToString());
                    fast.FastAuditresult = mes.Rows[i]["fast_auditresult"].ToString();
                    if (mes.Rows[i]["fast_auditstatus"].ToString() == "0")
                    {
                        fast.FastAuditstatus = "未审核";
                    }
                    else
                    {
                        fast.FastAuditstatus = "已审核";
                    }
                    fastlist.Add(fast);
                }

            }

            return fastlist;
        }
         
        /// <summary>
        /// 根据流水号号查询审核信息
        /// </summary>
        /// <param name="LSH"></param>
        /// <returns></returns>
        public static TbBstFastprocess GetAduitByLSH(string LSH)
        {
            string sql = "select * from tb_bst_fastprocess where FAST_LSH = :LSH";
            OracleParameter[] sp = { 
                                   new OracleParameter(":LSH",LSH)
                                   };
            DataTable mes = OrclHelper.GetDT(sql, sp);
            TbBstFastprocess fast = new TbBstFastprocess();
            if (mes != null && mes.Rows.Count > 0)
            {

                fast.FastLsh = mes.Rows[0]["fast_lsh"].ToString();
                fast.FastAddress = mes.Rows[0]["FAST_ADDRESS"].ToString();
                fast.FastLpna = mes.Rows[0]["FAST_LPNA"].ToString();
                fast.FastLpnb = mes.Rows[0]["FAST_LPNB"].ToString();
                fast.FastPhonea = mes.Rows[0]["FAST_PHONEA"].ToString();
                fast.FastPhoneb = mes.Rows[0]["FAST_PHONEB"].ToString();
                fast.FastUptime = Convert.ToDateTime(mes.Rows[0]["FAST_UPTIME"]);
                fast.FastAudittime = Convert.ToDateTime(mes.Rows[0]["fast_audittime"].ToString());
                fast.FastAuditresult = mes.Rows[0]["fast_auditresult"].ToString();
                if (mes.Rows[0]["fast_auditstatus"].ToString() == "0")
                {
                    fast.FastAuditstatus = "未审核";
                }
                else
                {
                    fast.FastAuditstatus = "已审核";
                }
            }



            return fast;
        }

        /// <summary>
        /// 根据地区获取该地区未审核信息的条数
        /// </summary>
        /// <param name="ad"></param>
        /// <returns></returns>
        public static int GetUnAuditByAd(string ad)
        {
            int num = 0;
            if (ad == "370100000000")
            {
                string sql = "select count(*) from TB_BST_FASTPROCESS where FAST_AUDITSTATUS = '0' and FAST_COMPLETE = '1'";
                num = Convert.ToInt32(OrclHelper.ExecuteScalar(sql));
                return num;
            }
            else
            {
                string sql = "select count(*) from TB_BST_FASTPROCESS where fast_address = :ad and FAST_AUDITSTATUS = '0' and FAST_COMPLETE = '1'";
                OracleParameter[] sp = { 
                                   new OracleParameter(":ad",ad)
                                   };
                num = Convert.ToInt32(OrclHelper.ExecuteScalar(sql, sp));
                return num;
            }
        }
        #endregion

        #region 获取流水号

        public static string GetLSH(string tablename)
        {
            string lshdate = string.Empty;
            string lsh = string.Empty;
            string lshmaxid = "0000";
            int isExist = 0;
            string isexcit = "select count(*) from tb_bst_lsh where tablename = :tablename";
            OracleParameter[] spExist = {
                                   new OracleParameter(":tablename",tablename)
                                 };
            isExist = Convert.ToInt32(OrclHelper.ExecuteScalar(isexcit, spExist));
            if (isExist == 0)  //判断该表是否存在 如果不存在添加新数据
            {
                string sqlinsert = "insert into tb_bst_lsh(tablename,lshdate,lshmaxid) values(:tablename,:lshdate,:lshmaxid)";
                OracleParameter[] insertsp = {
                                   new OracleParameter(":tablename",tablename),
                                   new OracleParameter(":lshdate",DateTime.Now.ToString("yyyyMMdd")),
                                   new OracleParameter(":lshmaxid","0002")
                                 };
                OrclHelper.ExecuteOracle(sqlinsert, insertsp);
                return DateTime.Now.ToString("yyyyMMdd") + "0001";
            }
            else
            {
                string sql = "select lshdate,lshmaxid from tb_bst_lsh where tablename = :tablename";
                OracleParameter[] sp = {
                                   new OracleParameter(":tablename",tablename)
                                 };
                OracleDataReader dr = OrclHelper.ExecuteReader(sql, sp);
                while (dr.Read())
                {
                    lshdate = dr["lshdate"].ToString();
                    lshmaxid = dr["lshmaxid"].ToString();
                }
                lsh = lshdate + lshmaxid;
                int lastnum = int.Parse(lsh.Substring(8));
                lastnum++;
                //如果日期变更 则将最大值置为0001并改变日期
                if (lshdate != DateTime.Now.ToString("yyyyMMdd"))
                {
                    string sqlupdate = "update tb_bst_lsh set lshdate = :lshdate,lshmaxid =:lshmaxid where tablename = :tablename";
                    OracleParameter[] updatesp = {
                                    new OracleParameter(":tablename",tablename),
                                    new OracleParameter(":lshdate",DateTime.Now.ToString("yyyyMMdd")),
                                    new OracleParameter(":lshmaxid","0002")
                                  };
                    OrclHelper.ExecuteOracle(sqlupdate, updatesp);
                    return DateTime.Now.ToString("yyyyMMdd") + "0001";
                }
                else  //如果日期未改变 则将最大值+1操作
                {
                    string sqlupdate = "update tb_bst_lsh set lshdate = :lshdate,lshmaxid =:lshmaxid where tablename = :tablename";

                    OracleParameter[] updatesp = {
                                    new OracleParameter(":tablename",tablename),
                                    new OracleParameter(":lshdate",DateTime.Now.ToString("yyyyMMdd")),
                                    new OracleParameter(":lshmaxid", lastnum.ToString("0000"))
                                  };
                    OrclHelper.ExecuteOracle(sqlupdate, updatesp);
                    return lsh;
                }
            }
        }
        #endregion
    }
}

